<?php
class Teacher_model extends MY_Model {

    function __construct() {
        parent::__construct("s_teacher");
    }
    
	/**
     * 公用查询程序
     * $condition['teacher_id'] = ?  //按照老师编号（社团注册注册时自动产生的id）
     * $condition['offset'] = ?
     * $condition['per_page'] = ?
     * @param $condition
     * @param $row_str_FLAG 仅对查询结果是单条的：若设为TRUE：单条自动转为字符串， 若设为FALSE：单条自动转为数组
     * @param $count_FLAG 若设为TRUE：计算查询结果数， 若设为FALSE：直接查询结果
     */
    function search_by_condition($condition, $row_str_FLAG = FALSE, $count_FLAG = FALSE) {
        $this->db->select('id, teacher_id, t_name, t_gender, t_birth, t_phone, t_qq');

        if (isset($condition['teacher_id'])) {
            $this->db->where('teacher_id', $condition['teacher_id']);
        }

        if (!$count_FLAG) {
        	
            if (isset($condition['offset'])) {
                if (isset($condition['per_page'])) {
                    $this->db->limit($condition['per_page'], $condition['offset']);
                } else {
                    $this->db->limit(PER_PAGE, $condition['offset']);
                }

            }

            $query = $this->db->get('s_teacher');
        	if ($query->num_rows() == 1) {
				if (!$row_str_FLAG) {
	            	return $query->result_array();
				} else {
					return $query->row_array();
            	}
			} elseif ($query->num_rows() > 1) {
				return $query->result_array();
			} else {
				return NULL;
			}
        } else {
            $this->db->from('s_teacher');
            return $this->db->count_all_results();
        }
    }
    
	function _get_by_teacher_id($teacher_id) {
		$this->db->select("*");
        $this->db->where("teacher_id", $teacher_id);
        $query = $this->db->get($this->table_name);
        return $query->row_array();
	}
}